package com.xgggh.app;


import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatDelegate;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.widget.TextView;

import com.xgggh.view.LoadingDialog;

import static android.Manifest.permission.CAMERA;

/**
 * Created by az on 2018/5/24.
 */

public  class RxBaseActivity extends RxActivity {
    protected final long windowDuration = 2; //2s

    static {
        AppCompatDelegate.setCompatVectorFromResourcesEnabled(true);
    }
    protected LoadingDialog getLoadingDialog(Context context) {
        LoadingDialog loadingDialog  = new LoadingDialog(context);
        return loadingDialog;
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }


    @Override
    public void setSupportActionBar(@Nullable Toolbar toolbar) {
        super.setSupportActionBar(toolbar);
        toolBar();
    }

    protected void toolBar(){
        if (getSupportActionBar()==null){
            return;
        }
        getSupportActionBar().setDisplayShowTitleEnabled(false);
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);
        //默认显示android:label
        TextView title= findViewById(android.R.id.title);
        if (title!=null){
            title.setText(getTitle());
        }
    }

    /**
     * 权限申请
     * @param permission 权限名称
     * @param requestCode 请求码
     * @return 是否申请成功
     */
    protected boolean mayRequestPermissions(String permission,int requestCode) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            return true;
        }
        if (checkSelfPermission(permission) == PackageManager.PERMISSION_GRANTED) {
            return true;
        }
        if (shouldShowRequestPermissionRationale(permission)) {
            // TODO: alert the user with a Snackbar/AlertDialog giving them the permission rationale
            // To use the Snackbar from the design support library, ensure that the activity extends
            // AppCompatActivity and uses the Theme.AppCompat theme.
        } else {
            requestPermissions(new String[]{CAMERA}, requestCode);
        }
        return false;
    }


    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case android.R.id.home:
                finish();
                return true;
            default:
                break;
        }
        return super.onOptionsItemSelected(item);
    }


}
